home *** CD-ROM | disk | FTP | other *** search
/ .net (French) 1996 November / .net Magazine (FR) - Issue 01 - Nov 1996.iso / mac / Edition Web / PageSpinner 1.2b2 / JavaScript Examples / Random Link Example < prev    next >
Text File  |  1996-07-06  |  3KB  |  128 lines

  1. <HTML><HEAD>
  2. <TITLE>JavaScript Random Link</TITLE>
  3.  
  4. <SCRIPT LANGUAGE="JavaScript">
  5. <!-- Beginning of JavaScript --------
  6. /* 
  7.     GetRandomURL
  8.     Written by Jerry Aman, Optima System, May 19, 1996.
  9.     Part of the PageSpinner distribution.
  10.  
  11.     We will not be held responsible for any unwanted 
  12.     effects due to the usage of this script or any derivative.  
  13.     No warrantees for usability for any specific application 
  14.     are given or implied.
  15.  
  16.     You are free to use and modify this script,
  17.     if credits are kept in the source code
  18. */
  19.  
  20.  
  21. function GetRandomURL()
  22. {
  23.     // Put relative or full URL's in the strings below
  24.     // You can increase the number of URL's to more than 5 by adding a
  25.     // string containing an URL in list below
  26.  
  27. var locationlist = new URLList (
  28.     "harpo.html",
  29.     "groucho.html",
  30.     "chico.html",
  31.     "zeppo.html",
  32.     "Scrolling Text Stationery"
  33. );
  34.  
  35.     num = Math.round ( ( rand.next() * (locationlist.count-1)) );
  36.  
  37.     return locationlist.list[num];
  38. }
  39.  
  40. function URLList ()
  41. {
  42.     var argv = URLList.arguments;
  43.     var argc = argv.length;
  44.     this.list = new Object();
  45.     for (var i = 0; i < argc; i++)
  46.     this.list[i] = argv[i];
  47.     this.count = argc;
  48.      return this;
  49. }
  50.  
  51. //*********************************************
  52. // Park-Miller Pseudo-Random Number Generator
  53. // JavaScript implementation by David N. Smith
  54. // of IBM's T J Watson Research Center
  55. //*********************************************
  56. function NextRandomNumber()
  57. {
  58.     var hi   = this.seed / this.Q;
  59.     var lo   = this.seed % this.Q;
  60.     var test = this.A * lo - this.R * hi;
  61.     if (test > 0)
  62.         this.seed = test;
  63.     else
  64.         this.seed = test + this.M;
  65.     return (this.seed * this.oneOverM);
  66. }
  67.  
  68. function RandomNumberGenerator() 
  69. {
  70.     var d = new Date();
  71.     this.seed = 2345678901 +
  72.     (d.getSeconds() * 0xFFFFFF) +
  73.     (d.getMinutes() * 0xFFFF);
  74.     this.A = 48271;
  75.     this.M = 2147483647;
  76.     this.Q = this.M / this.A;
  77.     this.R = this.M % this.A;
  78.     this.oneOverM = 1.0 / this.M;
  79.     this.next = NextRandomNumber;
  80.     return this;
  81. }
  82.  
  83. var rand = new RandomNumberGenerator();
  84.  
  85. // -- End of JavaScript code -------------- -->
  86. </SCRIPT>
  87.  
  88. </HEAD>
  89. <BODY>
  90. <H1>JavaScript Random Link</H1>
  91.  
  92. <B>This stationery page contains a JavaScript that selects a random URL</B>
  93. <P>
  94. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher.<BR>
  95. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  96. <HR>
  97. <P>
  98. The script is named <B>GetRandomURL</B> and it is placed in the HEAD section of the HTML document. The script is executed by clicking on a link containing a call to GetRandomURL(). Also note the <FONT COLOR="FF3366">custom text</FONT> in Netscape's status area.
  99. <P>
  100.  
  101. Example of this script picking a 
  102. <A HREF=""
  103.  onClick="this.href=GetRandomURL()"
  104.  onMouseOver="window.status='This link goes to a randomly selected page'; return true">random</A> page.
  105. <P>
  106. <B>How to use:</B><BR>
  107. Replace the filenames inside the script with your own URLs and edit this page contents inside the <BODY> section. (You can also copy the entire script to an existing page).
  108.  
  109. <XMP>function GetRandomURL()
  110. {
  111. var locationlist = new URLList (
  112.     "harpo.html",
  113.     "groucho.html",
  114.     "chico.html",
  115.     "zeppo.html",
  116.     "http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/"
  117.     );
  118. </XMP>
  119. <P>
  120. Use code similar to this to let the user execute the script:
  121. <XMP><A HREF=""
  122.  onClick="this.href=GetRandomURL()"
  123.  onMouseOver="window.status='This link takes you to some unknown place';
  124.  return true">Go</A> </XMP>
  125.  
  126. </BODY>
  127. </HTML>
  128.